package com.njlime.cms.controller.app;

import com.njlime.cms.entities.Live;
import com.njlime.cms.entities.LiveComment;
import com.njlime.cms.entities.LiveUp;
import com.njlime.cms.service.app.AppLiveCommentService;
import com.njlime.cms.service.app.AppLiveService;
import com.njlime.cms.service.app.AppLiveUpService;
import com.njlime.core.base.controller.BaseAppController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import com.njlime.core.web.AppWxInterceptor;
import com.njlime.member.entiies.MemberComment;
import com.njlime.member.entiies.TMember;
import com.njlime.member.service.app.AppMemberService;
import com.njlime.member.service.app.AppMemberTrackService;
import com.njlime.member.service.app.AppPublishReportService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * Created by Dean on 2017/10/10.
 */
@Controller
@RequestMapping(value = "/app/live")
public class AppLiveController extends BaseAppController {

    @Resource
    private AppLiveService appLiveService;
    @Resource
    private AppLiveCommentService appLiveCommentService;
    @Resource
    private AppLiveUpService appLiveUpService;
    @Resource
    private AppMemberTrackService appMemberTrackService;
    @Resource
    private AppMemberService appMemberService;
    @Resource
    private AppPublishReportService appPublishReportService;

    private Logger logger = Logger.getLogger(AppLiveController.class);

    /**
     * 直播首页信息回访
     *
     * @param pageBean
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public String liveList(HttpServletRequest request, PageBean pageBean, Live live) {
        Integer root_unit_id = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ROOT_UNIT_ID);
        live.setPublish_status(GlobalValue.PublishStatus.PUBLISHED);
        live.setRoot_unit_id(root_unit_id);
        return toJsonListData(appLiveService.list(pageBean, live));
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public String liveDetail(HttpServletRequest request, int id) {
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        Integer unit_id = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_UNIT_ID);
        if (uid != null) {
            appMemberTrackService.save(uid, GlobalValue.BusinessType.LIVE, id);
        }
        appLiveService.updateViewCount(id);
        Map<String,Object> bean = appLiveService.get(id, uid);
        Integer business_unit = Integer.valueOf(bean.get("unit_id").toString());
        appPublishReportService.save(GlobalValue.BusinessType.LIVE,id,unit_id,business_unit);
        return toJsonSuccessData(bean);

    }

    /**
     * 获取社区评论列表
     */
    @ResponseBody
    @RequestMapping(value = "/commentList")
    public String commentList(HttpServletRequest request, PageBean pageBean, LiveComment communityComment) {
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        return toJsonListData(appLiveService.commentList(pageBean, communityComment, uid));

    }

    /**
     * 添加评论
     * @param request
     * @param liveComment
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/commentMake")
    public String commentMake(HttpServletRequest request, LiveComment liveComment) {
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        String name = (String) request.getSession().getAttribute(AppWxInterceptor.WX_USER_NAME);
        try {
            if (uid == null) {
                return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "请登录后添加评论");
            } else {
                TMember member = appMemberService.findOne(uid);
                Live live = appLiveService.findOne(liveComment.getLive_id());
                liveComment.setIs_admin(GlobalValue.NO);
                if (!StringUtil.isEmpty(live.getMobiles()) && !StringUtil.isEmpty(member.getMobile())) {
                    if (live.getMobiles().indexOf(member.getMobile()) >= 0) {
                        liveComment.setIs_admin(GlobalValue.YES);
                    }
                }
                liveComment.setUser_id(uid);
                liveComment.setUser_name(name);
                liveComment.setCreator(uid);
                liveComment.setCreate_time(new Date());
                liveComment.setFabulous_num(0);
                liveComment.setIs_top(GlobalValue.NO);
                liveComment.setIs_del(GlobalValue.NO);
                if(live.getIs_comment() == GlobalValue.YES){
                    liveComment.setStatus(GlobalValue.UNAUDIT);
                    appLiveService.addComment(liveComment, null);
                    return toJsonErrorData(ExceptionState.NEED_AUDIT_EXCEPTION,"添加成功，请等待审核");
                }else{
                    liveComment.setStatus(GlobalValue.AUDITED);
                    //用户评论
                    MemberComment memberComment = new MemberComment();
                    memberComment.setMember_id(liveComment.getUser_id());
                    memberComment.setBusiness_id(liveComment.getLive_id());
                    memberComment.setBusiness_type(GlobalValue.BusinessType.LIVE);
                    memberComment.setBusiness_title(live.getTitle());
                    memberComment.setCreate_time(new Date());
                    appLiveService.addComment(liveComment, memberComment);
                }
                return toJsonSuccessData();
            }

        } catch (Exception e) {
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "评论添加失败");
        }
    }

    /**
     * 点赞
     */
    @ResponseBody
    @RequestMapping(value = "upAdd")
    public String upAdd(HttpServletRequest request, LiveUp liveUp) {
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        try {
            if (uid == null) {
                return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "用户未登录");
            } else {
                if (appLiveUpService.isUped(liveUp.getComment_id(), uid)) {
                    appLiveUpService.del(liveUp.getComment_id(), uid);
                    return toJsonSuccessData();
                } else {
                    liveUp.setCreate_time(new Date());
                    liveUp.setUp_user_id(uid);
                    liveUp.setLive_id(liveUp.getLive_id());
                    appLiveUpService.save(liveUp);
                    return toJsonSuccessData();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "点赞失败");
        }
    }

}
